{{define "admin/menu.html"}}

<!DOCTYPE html>
<html>

<head>
  {{template "layout/head.html" .}}
  <style type="text/css">
    td span {
        cursor: pointer
    }
    .menu-data-list-ul{
        list-style: none;
        padding: 10px 0px;
    }
    .menu-data-list-ul li{
        list-style: none;
        border-bottom: 1px solid #f4f4f4;
        font-size: 14px;
        padding: 10px;
    }
    .menu-data-list-ul li:hover{
        background-color: #CCCCCC;
    }
    .menu-data-list-ul li a{
        color: #444444;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .menu-data-list-ul li a .btns{
        /* float: right; */
        margin-right: 10px;
     }
    .menu-data-list-ul li a .btns i{
        padding: 5px 5px;
    }
    .treetable-indent{
        width: 16px;
        height: 16px;
        display: inline-block;
        position: relative;
    }
    .bootstrap-tree-table .treetable-box .treetable-fixed{
        z-index: auto;
    }
    .bootstrap-select-searchbox .input-block-level{
        width:100%;
    }
</style>
</head>

<body>
    <div class="wrapper">
        <div class="content-wrapper">
            <div class="container-fluid">
                <div class="card card-default">
                    <div class="card-header" style="display: none;">
                        <div class="row " class="margin-left: 0px;">
                            <div class="searchPanel form-horizontal form-inline" >                                  
                                
                                <label class="control-label" style="padding-right:10px;text-align: right; ">菜单:</label>
                                <div class="btn-group menuIDSelect">
                                    <button type="button" data-name="menuid" class="btn btn-default ">
                                    </button>
                                    <button type="button" class="btn btn-default  dropdown-toggle"     data-toggle="dropdown">
                                        <span class="caret"></span>
                                    </button>
                                    <ul class="dropdown-menu" role="menu" style="max-width: 300px;">
                                        <li class="header">
                                            <button class="btn bg-olive btn-flat margin add_menu_btn" style="width:100%;">添加</button>
                                        </li>
                                        <li>
                                            <!-- inner menu: contains the actual data -->
                                            <ul class="menu menu-data-list-ul">
                                            </ul>
                                        </li>
                                        <li class="footer">
                                            <div id="menu_pagelist">
                                                <div id="menu_myPage" class="pagination pull-right"></div>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            
        
                        </div>
        
                    </div><!-- /.box-header -->
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-12">
                                <table class="table table-bordered table-hover menu_data_list_table table-responsive">
        
                                </table>
        
                            </div>
                        </div>
                    </div><!-- /.box-body -->
                </div>
            </div>
        </div>
       
        <div class="content" style="display: none">
            
                <div class="addMenuDivTemplate" style="display: none">
                    <form class="form-horizontal">
                        <div class="box-body" style="overflow-x: hidden;">
                            <div class="form-inline row"><label class="col-sm-3 control-label">类型：</label>
                                <div class="col-sm-9"><select class="feild form-control  control-block"  feildName="pType"></select></div>
                            </div>
                            <div class="form-inline row"><label class="col-sm-3 control-label">菜单名称：</label>
                                <div class="col-sm-9"><input type="text" class="feild form-control  control-block "  feildName="pName"  value=""/></div>
                            </div>
                            <div class="form-inline row"><label class="col-sm-3 control-label">描述：</label>
                                <div class="col-sm-9"><input type="text"  class="feild form-control  control-block "  feildName="pDesc" value=""/></div>
                            </div>
                            <div class="form-inline row"><label class="col-sm-3 control-label">排序：</label>
                                <div class="col-sm-9"><input type="number" class="feild form-control  control-block "  feildName="pSort" value="" /></div>
                            </div>
                            <div class="form-inline row"><label class="col-sm-3 control-label">图标：</label>
                                <div class="col-sm-9"><input type="text" class="feild form-control  control-block "  feildName="pIcon" value="" /></div>
                            </div>
        
                            <div class="form-inline row"><label class="col-sm-3 control-label">状态：</label>
                                <div class="col-sm-1"><label><input type="checkbox" class="feild form-control  "  feildName="pState" />启用</label></div>
                            </div>
                            <div class="form-inline row"><label class="col-sm-3 control-label">应用：</label>
                                <div class="col-sm-9">
                                    <select feildName="pAppid" class="form-control  feild appidSelect" style="width:100%">
                                    </select>
                                </div>
                            </div>
                            <div class="form-inline row"><label class="col-sm-3 control-label">请求地址：</label>
                                <div class="col-sm-9"><select class="feild form-control  control-block  selectpicker"  feildName="pUrl"></select></div>
                            </div>
                            <div class="form-inline row"><label class="col-sm-3 control-label">权限标识：</label>
                                <div class="col-sm-9"><select class="feild form-control  control-block "  feildName="pPermission"></select></div>
                            </div>
                            <div class="form-inline row"><label class="col-sm-3 control-label">自定义参数：</label>
                                <div class="col-sm-9"><input type="text" class="feild form-control  control-block "  feildName="pParams" value="" /></div>
                            </div>
                        </div>
                    </form>
                </div>
            
            
                <div class="col-md-12" style="display: none;overflow-y: auto;" id="MenuRightPannel">
                    <div class="ztree">
                    </div>
                </div>
            
            
        </div>
            
    </div>

  {{template "layout/foot.html" .}}
  {{template "layout/iframePageFoot.html" .}}

  <link href="{{.staticResourcesPathJs}}/static/plugins/jquery.treegrid/css/jquery.treegrid.css" rel="stylesheet">
  <script src="{{.staticResourcesPathJs}}/static/plugins/jquery.treegrid/js/jquery.treegrid.min.js"></script>
  
    <!-- iCheck -->
  <script src="{{.staticResourcesPathJs}}/static/plugins/iCheck/icheck.min.js"></script>
  <link href="{{.staticResourcesPathJs}}/static/plugins/iCheck/all.css" type="text/css" rel="stylesheet"/>

  
  <link rel="stylesheet" type="text/css" href="{{.staticResourcesPathJs}}/static/plugins/bootstrap-select/css/bootstrap-select.min.css">
  <script type="text/javascript" src="{{.staticResourcesPathJs}}/static/plugins/bootstrap-select/js/bootstrap-select.min.js"></script>

  <link href="{{.staticResourcesPathJs}}/static/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
  <script src="{{.staticResourcesPathJs}}/static/plugins/bootstrap-table/bootstrap-table.js"></script>
  <script src="{{.staticResourcesPathJs}}/static/plugins/bootstrap-table/bootstrap-table-locale-all.min.js"></script>
  <script src="{{.staticResourcesPathJs}}/static/plugins/bootstrap-table/extensions/treegrid/bootstrap-table-treegrid.min.js"></script>
  <script type="text/javascript">
    /**
     * Created by Eric.Zhang on 2016/12/31.
     */
    var Menu_operat = {
        menuid: 0,
        rootID:0,
        defaultAppid:0,
        pTypes_data_operat:{
            valueNames : [
                {value: 2, name: '菜单'},
                {value: 3, name: '页面'}],
            getName : function (value) {
                var result = '';
                $(this.valueNames).each(function (i, vn) {
                    if (vn.value == value) {
                        result = vn.name;
                        return false;
                    }
                });
                return result;
            }
        }
        ,apps:{
            datas:[],
            getName : function (value) {
                var result = '';
                $(this.datas).each(function (i, vn) {
                    if (vn.pId == value) {
                        result = vn.pName;
                        return false;
                    }
                });
                return result;
            }
        }
        ,menuModels:[]
    }

    Menu_operat.pageInit = function () {  
        $('.menuIDSelect .add_menu_btn').click(function () {
            var model={
                pMenuID:0,
                pPid:0,
                pState:1,
                pType:2,
                pAppid:Menu_operat.defaultAppid,
                pSort:0,
                pLevel:0
            }
            Menu_operat.addOrUpdateMenu(model,true,function (m) {
                if($('.menuIDSelect .menu-data-list-ul li').length==0){
                    Menu_operat.MenuSelectchange(m)
                }
                Menu_operat.showMenuPage(m);
            })
        })
        $(Menu_operat.pTypes_data_operat.valueNames).each(function (i,vm) {
            $('.addMenuDivTemplate .feild[feildName="pType"]').append('<option value="'+vm.value+'" '+(i==0?'selected':'')+'>'+vm.name+'</option>');
        })
        Menu_operat.InitAppsData(function(){
            Menu_operat.showMenuList(1)
        })
    }
    //region 头部选择内容

    Menu_operat.showMenuList = function (pageIndex) {
        var pageSize = 100;
        $('.menuIDSelect .btn[data-name="menuid"]').attr('data-value',0).html('无');
        Menu_operat.menuid=0;
        $('.menuIDSelect .menu-data-list-ul').empty();
        $('.menu_data_list_table tbody').empty();
        $.ajax({
            url: page_content_path + '/api/system/authorize/menu/getRootByPage',
            type: "get",
            data: {pageIndex: pageIndex, pageSize: pageSize},
            cache: false,
            success: function (data) {
                if (joyconn_layout.ValidataResult(data)) {
                    $(data.data).each(function (i, model) {
                        Menu_operat.showMenuPage(model)
                        if (i == 0) {
                            Menu_operat.MenuSelectchange(model);
                        }
                    })
                    ShowPage('menu_myPage', {
                        pno: pageIndex,
                        pagesize: pageSize,
                        allcount: data.allcount
                    }, function (pno) {
                        Menu_operat.showMenuList(pno);
                    });
                }

            }
        });
    }
    Menu_operat.showMenuPage = function (model) {
        var temp;
        temp = $('.menuIDSelect .menu-data-list-ul li[data-value="'+model.pId+'"]');
        if(!temp||temp.length==0){
            temp = $(' <li>\n' +
                '               <a href="javascript:void(0)"><span style="padding-right: 10px;padding-left: 10px" class="menu_item_id  bg-navy "></span><span class="menu_item_name"></span>\n' +
                '                     <span  class="btns"><i class="fa fa-edit"></i><i class="fa fa-remove "></i></span>\n' +
                '               </a>\n' +
                '           </li>   ')
            $('.menuIDSelect .menu-data-list-ul').append(temp);
        }

        temp.attr('data-value', model.pId).unbind('click').click(function () {
            if ($('.menuIDSelect .btn[data-name="menuid"]').attr('data-value') != model.pId) {
                Menu_operat.MenuSelectchange(model);
            }
        });
        temp .find('.menu_item_id').html( 'id : '+model.pId);
        temp .find('.menu_item_name').html( model.pName).attr('title', model.pDesc);
        temp.find('.fa-remove').unbind('click').click(function () {
            Menu_operat.deleteMenu(model,function (m) {
                temp.remove();
                if($('.menuIDSelect .menu-data-list-ul li').length==0){
                    Menu_operat.MenuSelectchange({pId:0,pName:'无'})
                }
                
            })
            return false;
        })
        temp.find('.fa-edit').unbind('click').click(function () {
            Menu_operat.addOrUpdateMenu(model,false,function (m) {
                Menu_operat.showMenuPage(m);
            })
            return false;
        })
    }

    Menu_operat.MenuSelectchange = function (model) {
        $('.menuIDSelect .btn[data-name="menuid"]').attr('data-value', model.pId).html(model.pName);
        Menu_operat.menuid = model.pId;
        Menu_operat.showList();
    }



    //endregion


    //region menu内容
  
    Menu_operat.showList = function () {
        if(Menu_operat.menuid>0){
            $.ajax({
                url: page_content_path + '/api/system/authorize/menu/getMenu',
                type: "get",
                data: {menuID:Menu_operat.menuid},
                cache: false,
                success: function (data) {
                    if (joyconn_layout.ValidataResult(data)) {
                        var models = data.data;
                        $(models).each(function (i,m1) {
                            if(m1.pMenuID==0){
                                Menu_operat.rootID=m1.pId ;
                                return false;
                            }
                        })
                        Menu_operat.ShowMenuNodesByTreeTable(models)

                    }

                }
            });
        }

    }
    Menu_operat.ShowMenuNodesByTreeTable = function (models){        
        Menu_operat.menuModels=models;
        $('.menu_data_list_table').bootstrapTable('destroy')
        var treeTable =$('.menu_data_list_table');
        treeTable.bootstrapTable({
            idField: 'pId',                                                   // 选取记录返回的值,用于设置父子关系
            parentIdField: 'pPid',  
            toolbar:".searchPanel.form-horizontal.form-inline",                                     // 用于设置父子关系,
            classes:'table table-bordered table-hover menu_data_list_table table-responsive treetable',
            showColumns: true,
            treeShowField: 'pName',
            data:Menu_operat.menuModels,
            columns: [
                [
                //     {
                //     title: 'id',
                //     field: 'indexxxx',
                //     formatter: function(value,model, index) {
                //     return index
                //     }
                // },
                {
                        title: '类型',
                        field: 'pType',
                        align: "center",
                        colspan:1,
                        width: '3',
                        widthUnit:'%',
                        formatter: function(value,model, index) {
                            var text =  Menu_operat.pTypes_data_operat.getName(value)
                            return ' <span class="nav-text" title="'+text+'">' + text + '</span>';
                        }
                    },
                {
                title: '菜单节点名称',
                field: 'pName',
                width: '20',
                colspan:1,
                widthUnit:'%',
                formatter: function(value,model, index) {
                    if(model.pMenuID==0){
                        return '<i class="fa fa-sitemap text-green"></i> <span class="nav-label  text-green" title="'+model.pDesc+'">' + model.pName + '<br />（菜单根节点）</span>';
                    }else{
                        if (model.pIcon == null || model == "") {
                            return model.pName;
                        } else {
                            return '<i class="' + model.pIcon + '"></i> <span class="nav-label" title="'+model.pDesc+'">' + model.pName + '</span>';
                        }
                    }
                    if (model.pIcon == null || model == "") {
                        return model.pName;
                    } else {
                        return '<i class="' + model.pIcon + '"></i> <span class="nav-label" title="'+model.pDesc+'">' + model.pName + '</span>';
                    }
                }
            },{
                title: '状态',
                field: 'pState',
                align: "center",
                colspan:1,
                width: '3',
                widthUnit:'%',
                formatter: function(value,model, index) {
                    var text =   value==1?'启用':'隐藏';
                    return ' <span class="nav-text" title="'+text+'">' + text + '</span>';

                }
            },{
                title: '排序',
                field: 'pSort',
                width: '3',
                colspan:1,
                widthUnit:'%',
                formatter: function(value,model, index) {
                    return ' <span class="nav-text" title="'+value+'">' + value + '</span>';
                }
            },
            {
                title: '应用',
                field: 'pAppid',
                align: "center",
                colspan:1,
                width: '3',
                widthUnit:'%',
                formatter: function(value,model, index) {
                    var text =  Menu_operat.apps.getName(value)
                    return ' <span class="nav-text" title="'+text+'">' + text + '</span>';
                }
            },{
            title: '请求地址/权限标识',
            field: 'pUrl',
            width: '25',
            colspan:1,
            widthUnit:'%',
            class:'ellipsis',
            formatter: function(value,model, index) {
                return   '<div class="nav-text text-primary" title="'+value+'">' + value + '</div>'
                +'<div class="nav-text text-muted" title="'+model.pPermission+'">' + model.pPermission + '</div>';
            }
            // },{
            //     title: '权限标识',
            //     field: 'pPermission',
            //     width: '20',
            //     colspan:1,
            //     widthUnit:'%',
            //     class:'ellipsis',
            //     formatter: function(value,model, index) {
            //         return ' <span class="nav-label " title="'+value+'">' + value + '</span>';
            //     }
            },{
                title: '自定义参数',
                field: 'pParams',
                width: '11',
                colspan:1,
                widthUnit:'%',
                class:'ellipsis',
                formatter: function(value,model, index) {
                    return ' <div class="nav-text " title="'+value+'">' + value + '</div>';
                }
            },{
                title: '操作',
                field: 'operate',
                align: "center",
                colspan:1,
                width: '8',
                widthUnit:'%',
                events:'menu_table_node_click',
                formatter: function(value,model, index) {                    
                    return '<span>'
                        +'<a class="btn btn-success btn-xs edit_btn" href="javascript:void(0)"><i class="fa fa-edit"></i>编辑</a> '
                        +'<a class="btn btn-info btn-xs add_btn" href="javascript:void(0)"><i class="fa fa-plus"></i>新增</a> '
                        +'<a class="btn btn-danger btn-xs delete_btn" href="javascript:void(0)" ><i class="fa fa-remove"></i>删除</a> '
                        +'</span>';
                }
            }]]
            ,onPostBody: function() {
                var columns = treeTable.bootstrapTable('getOptions').columns
                if (columns && columns[0][1].visible) {
                    treeTable.treegrid({
                        treeColumn: 1,
                        onChange: function() {
                            treeTable.bootstrapTable('resetView')
                        }
                    })
                }
            }
        });
    }
    window.menu_table_node_click={
        'click .edit_btn':function(e, value, row, index){
            var model = row;
            Menu_operat.addOrUpdateMenu(model,false,function (m) {
                $(Menu_operat.menuModels).each(function (i,m1) {
                    if(m1.pId==m.pId){
                        Menu_operat.menuModels.splice(i,1,m);
                        return false;
                    }
                })
                // $('.menu_data_list_table').bootstrapTable('refresh');
                $('.menu_data_list_table').bootstrapTable('updateRow',{index:index,row:m});
            })
        }
        ,'click .add_btn':function(e, value, row, index){
            var model = row;
            var addModel={
                pMenuID:model.pMenuID==0?model.pId:model.pMenuID,
                pPid:model.pId,
                pState:1,
                pSort:1,
                pLevel:model.pLevel+1
            }
            Menu_operat.addOrUpdateMenu(addModel,true,function (m) {
                Menu_operat.menuModels.push(m);
                // $('.menu_data_list_table').bootstrapTable('refresh');
                $('.menu_data_list_table').bootstrapTable('insertRow',{index:index,row:m});
            })
        }
        ,'click .delete_btn':function(e, value, row, index){
            var model = row;
            Menu_operat.deleteMenu(model, function (m) {
                $( Menu_operat.menuModels).each(function (i,m1) {
                    if(m1.pId==m.pId){
                        Menu_operat.menuModels.splice(i,1)  ;
                        return false;
                    }
                })
                // $('.menu_data_list_table').bootstrapTable('refresh');
                $('.menu_data_list_table').bootstrapTable('remove',{field:'pId',values:[model.pId]});
            });
        }
    }


    Menu_operat.addOrUpdateMenu = function (model,isadd,callback) {
        var disElement = $($(".addMenuDivTemplate").html());
        var dia = JoyDialog({
                titleText: (isadd?'添加':'修改')+'菜单',
                content: disElement.get(0),
                // width: '400px',
                buttonTextCancel:"关闭",
                buttonTextConfirm:  (isadd?'添加':'修改'),
                onClickConfirmBtn: ClickConfirmBtn
            });
            function ClickConfirmBtn(){
                var postModel = JSON.parse(JSON.stringify(model));
                postModel.children=[]
                disElement.find('.feild').each(function (i,span) {
                    var feildName = $(span).attr('feildName');
                    if(feildName=='pState'){
                        postModel[feildName]=$(span).is(':checked')?1:0;
                    }else if(feildName=='pType'){
                        postModel[feildName]=$(span).val()*1;
                    }else if(feildName=='pSort'){
                        postModel[feildName]=$(span).val()*1;
                    }else if(feildName=='pAppid'){
                        postModel[feildName]=$(span).val()*1;
                    }else{
                        postModel[feildName]=$(span).val();
                    }
                })
                if (postModel.pName.trim().length == 0) {
                    dialog({
                        titleText: '错误',
                        content: '菜单名称不能为空！',
                        quickClose: true,
                    }).show();
                    JoyDialog({
                                    type:"notice_error",
                                    content: '菜单名称不能为空！'
                                });
                    return false;
                }
                var url = page_content_path + '/api/system/authorize/menu/'+(isadd?'add':'update');
                $.ajax({
                    url: url,
                    data: JSON.stringify(postModel),
                    contentType:'application/json',
                    type: 'post',
                    success: function (data) {
                        if (!joyconn_layout.ValidataResult(data)) {
                            return;
                        }
                        if (data.data) {
                                JoyDialog({
                                    titleText: '成功',
                                    content: (isadd?'添加':'修改')+'菜单成功！',
                                    buttonTextCancel:false,
                                });
                                dia.close()
                                if(callback){
                                    postModel.children=model.children;
                                    if(isadd){
                                        postModel.pId=data.data;
                                    }
                                    callback(postModel);
                                }
                        }else if (data.code == 100111) {
                            JoyDialog({
                                type:"toast_warning",
                                infoText: data.msg,
                            });
                        } else {
                                JoyDialog({
                                    titleText: '错误',
                                    content: (isadd?'添加':'修改')+ '菜单失败！',
                                    buttonTextCancel:false
                                });
                            
                        }
                    }
                });
                return false;
            }
            
            //region appid
            $(disElement).find('select.feild[feildName="pAppid"]').empty().unbind('change').change(function () {
                var appid = $(container).find('select.feild[feildName="pAppid"]').selectpicker('val');
                Menu_operat.initUrlSelect(appid,disElement,"")
            })    
            $.each(Menu_operat.apps.datas,function(i,app){
                $(disElement).find('select.feild[feildName="pAppid"]').append('<option '+(i==0?"selected":"")+' value="'+app.pId+'" title="'+app.pDesc+'">'+app.pName+'</option>')
            })
            $(disElement).find('select.feild[feildName="pAppid"]').selectpicker({
                noneSelectedText: '未选择',
                liveSearch: true,
                style: 'btn-default btn-white',
                size:5
            });
            $(disElement).find('.bootstrap-select').css("width","100%")
            //endregion

            disElement.find('input[type="checkbox"]').iCheck({
                checkboxClass: 'icheckbox_square-blue',
                radioClass: 'iradio_square-blue',
                increaseArea: '20%' // optional
            });
            if(!isadd){
                disElement.find('.feild').each(function (i,span) {
                    var feildName = $(span).attr('feildName');
                    if(model[feildName]!=undefined){
                        if(feildName=='pState'){
                            disElement.find('input[type="checkbox"]').iCheck(model[feildName]==1?'check':'uncheck');
                        }else if(feildName=="pUrl"){
                        }else{
                            $(span).val(model[feildName]);
                        }
                    }
                })
                Menu_operat.initUrlSelect(model.pAppid,disElement,model.pUrl)    
            }else{
                
                Menu_operat.initUrlSelect(Menu_operat.defaultAppid,disElement,"")    
            }
    }


    Menu_operat.deleteMenu = function (model,callback) {
        var dia = JoyDialog({
                titleText:'删除',
                content: '您要删除'+(model.pMenuID==0?'整个':'')+'菜单:'+model.pName+'吗?',
                width: '40em',
                buttonTextCancel:false,
                buttonTextConfirm:  '删除',
                onClickConfirmBtn: function () {
                var url = page_content_path + '/api/system/authorize/menu/'+(model.pMenuID==0?'deleteMenu':'deleteNode');
                var postData=model.pMenuID==0?{
                                            menuID:model.pId
                                        }:{
                                            menuID:model.pMenuID,
                                            pId:model.pId
                                        };

                $.ajax({
                    url: url,
                    data: postData,
                    type: 'post',
                    success: function (data) {
                        if (!joyconn_layout.ValidataResult(data)) {
                            return;
                        }
                        if (data.data>0) {
                                JoyDialog({
                                    titleText: '成功',
                                    content:'添加删除成功！',
                                    buttonTextCancel:false
                                });
                                dia.close()
                            if(callback){
                                callback(model);
                            }
                        } else {
                                JoyDialog({
                                    titleText: '错误',
                                    content: '删除菜单失败！',
                                    buttonTextCancel:false
                                });
                        }
                    }
                });
                return false;
                }
        })

    }
    //禁用、启用菜单
    Menu_operat.upataMenuState = function (sender, menuId) {
        var menuModel = Menu_operat.getMenuInPage(menuId);
        var pState = $(sender).text() == "启用" ? 1 : 0;
        var text = $(sender).text() == "启用" ? "禁用" : "启用";
        var dia = JoyDialog({
                titleText:$(sender).text() + '菜单',
                content: '确认' + $(sender).text() + '菜单吗？',
                width: '40em',
                buttonTextCancel:false,
                buttonTextConfirm:   $(sender).text(),
                onClickConfirmBtn: function () {
                $.ajax({
                    url: page_content_path + '/api/system/authorize/menu/updateState',
                    data: {Id: menuId, state: pState},
                    type: 'post',
                    success: function (data) {
                        if (!joyconn_layout.ValidataResult(data)) {
                            return;
                        }
                        if (data.data > 0) {
                            JoyDialog({
                                    titleText: '成功',
                                    content: '菜单成功！',
                                    buttonTextCancel:false
                                });
                            dia.close()
                            $(sender).text(text);

                        }else if (data.code == 100111) {
                            JoyDialog({
                                type:"toast_warning",
                                infoText: data.msg,
                            });
                        } else {
                                JoyDialog({
                                    titleText: '错误',
                                    content: '菜单失败！',
                                    buttonTextCancel:false
                                });
                        }
                    }
                });
                return false;
                }
        })
    }

    Menu_operat.InitAppsData = function (callback) {
        $.ajax({
            url:  page_content_path+'/api/system/authorize/app/getByPage',
            data:{pageIndex:1,pageSize:10000000,state:1},
            type: 'get',
            cache: true,
            success: function (result) {
                if (!joyconn_layout.ValidataResult(result)) {
                    return;
                }
                if (result.data){
                    Menu_operat.apps.datas=result.data;
                    if(Menu_operat.apps.datas.length>0){
                        Menu_operat.defaultAppid = Menu_operat.apps.datas[0].pId
                    }
                }
                if(callback){
                    callback();
                }
            }

        });
    }
    
    Menu_operat.menuNodeUrlManage = function (appid,callback) {
        $.ajax({
            url:  page_content_path+'/api/system/authorize/resource/getAllPermission?appid='+appid,
            type: 'get',
            cache: true,
            success: function (result) {
                if (!joyconn_layout.ValidataResult(result)) {
                    return;
                }
                if (result.data){
                    var urlPermissionMap={};
                    var urls=[];
                    for (var a = 0; a < result.data.length; a++) {
                        var pathNode = result.data[a];
                        if(!pathNode){continue;}
                        $(pathNode).each(function (i,info) {
                            if(info.isApi){
                                return
                            }
                            urls.push(info.url);
                            if(typeof(urlPermissionMap[info.url])=="undefined"){
                                urlPermissionMap[info.url]=[]
                            }
                            urlPermissionMap[info.url].push(info.permission);
                        })
                    }
                    if(callback){
                        callback(urlPermissionMap,urls)
                    } 
                }
            }

        });
    }
    Menu_operat.initUrlSelect=function (appid,container,val) {
        Menu_operat.menuNodeUrlManage(appid,function(urlPermissionMap,urls){
            $(container).find('select.feild[feildName="pUrl"]').empty().append('<option value="#" selected>无</option>').unbind('change').change(function () {
                var url = $(container).find('select.feild[feildName="pUrl"]').selectpicker('val');
                Menu_operat.initPerMissionSelect(url,urlPermissionMap,container);
            })            
            Menu_operat.initPerMissionSelect('#',urlPermissionMap,container);
            $(urls).each(function (i,u) {
                $(container).find('select.feild[feildName="pUrl"]').append('<option value="'+u+'" >'+u+'</option>');
            })
            $(container).find('select.feild[feildName="pUrl"]').selectpicker({
                noneSelectedText: '未选择',
                liveSearch: true,
                style: 'btn-default btn-white',
                size:5
            });
        })
        
    }
    
    Menu_operat.initPerMissionSelect=function (url,urlPermissionMap,container) {
        $(container).find('select.feild[feildName="pPermission"]').empty();
        if(url=='#'){
            $(container).find('select.feild[feildName="pPermission"]').append('<option value="" selected>无</option>')
        }else{
            var permissions=urlPermissionMap[url];
            if(permissions&&permissions.length>0){
                $(permissions).each(function (i,p) {
                    $(container).find('select.feild[feildName="pPermission"]').append('<option value="'+p+'" '+(i==0?'selected':'')+'>'+p+'</option>');
                })
            }else{
                $(container).find('select.feild[feildName="pPermission"]').append('<option value="" selected>无</option>')
            }
        }
    }
    function FillnodeName(rightids, nodes) {

        for (var i = 0; i < nodes.length; i++) {

            nodes[i].name = nodes[i].pName;
            // nodes[i].children = nodes[i].Children;
            nodes[i].title = nodes[i].pDesc;
            nodes[i].tId = nodes[i].pId;

            nodes[i].open = true;
            if ($.inArray(nodes[i].pId + '', rightids) > -1) {
                nodes[i].checked = true
                // if(nodes[i].getParentNode()){
                //     nodes[i].getParentNode().open=true;
                // }
            }
            if (nodes[i].children && nodes[i].children.length > 0) {
                FillnodeName(rightids, nodes[i].children);
            }

        }
    }
    //endregion



    $(function () {  
        Menu_operat.pageInit();
    });
</script>
</body>

</html>
{{end}}